package com.sailfish.springbootdemo.controller.db2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.*;
import com.sailfish.springbootdemo.service.db2.*;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/pcbCapacityMatch")
public class CapacityMatchController {

    @Autowired
    private CapacityMatchService capacityMatchService;

    @PostMapping("/getData")
    public Result getData(@RequestBody String data) {
        List<CapacityMatch> all = capacityMatchService.getAll(data);
        return ResultUtil.success(all);
    }

    @PostMapping("/getMatchParam")
    public Result getMatchParam(Integer status) {
        List<PcbSizeParam> matchParam = capacityMatchService.getMatchParam(status);
        return ResultUtil.success(matchParam);
    }

    @PostMapping("/getMatchParamByKey")
    public Result getMatchParamByKey(String key) {
        PcbSizeParam matchParam = capacityMatchService.getMatchParamByKey(key);
        return ResultUtil.success(matchParam);
    }

    @PostMapping("/getMatchFactoryValue")
    public Result getMatchFactoryValue(@RequestBody List<Integer> factoryInfos,String searchK,String searchV) {
        List<CapacityMatchData> matchParam = capacityMatchService.getMatchFactoryValue(factoryInfos, 1);
        return ResultUtil.success(matchParam);
    }

    @PostMapping("/getMatchFactoryValueAll")
    public Result getMatchFactoryValueAll(@RequestBody List<Integer> factoryInfos) {
        List<CapacityMatchData> matchParam = capacityMatchService.getMatchFactoryValue(factoryInfos, 2);
        return ResultUtil.success(matchParam);
    }

    @PostMapping("/getMatchFactoryValueByFactoryId")
    public Result getMatchFactoryValueByFactoryId(Integer sonFactoryInfoId) {
        List<PcbSizeFactoryValue> matchParam = capacityMatchService.getPcbMatchFactoryValue(sonFactoryInfoId);
        return ResultUtil.success(matchParam);
    }

    @PostMapping("/insertOrUpdateMatchParam")
    public Result insertOrUpdateMatchParam(@RequestBody String data) {
        return capacityMatchService.insertOrUpdateMatchParam(data);
    }

    @PostMapping("/deleteMatchParam")
    public Result deleteMatchParam(Integer id) {
        return capacityMatchService.deleteMatchParam(id);
    }

    @PostMapping("/insertOrUpdateMatchFactoryValue")
    public Result insertOrUpdateMatchFactoryValue(@RequestBody String data) {
        return capacityMatchService.insertOrUpdateMatchFactoryValue(data);
    }


    @PostMapping("/insertOrUpdateMatchFactoryValue2")
    public Result insertOrUpdateMatchFactoryValue2(@RequestBody String data) {
        return capacityMatchService.insertOrUpdateMatchFactoryValue2(data);
    }

    @PostMapping("/outputParamTemplate")
    public Result outputParamTemplate(@RequestBody String data) throws IOException {
        Result result = capacityMatchService.outputParamTemplate(data);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);  //InputStream in = url.openStream();
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);

            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }

//    @PostMapping("getAllApproveManagement")
//    public Result getAllApproveManagement(){
//         List<PcbApproveManagement> allApproveManagement = capacityMatchService.getAllApproveManagement();
//         return ResultUtil.success(allApproveManagement);
//    }

    //获取认证数据
    @PostMapping("getAuthencationData")
    public Result getAuthencationData(Integer approveStatus) {
        List<PcbOpenCapacityAuthentication> authencationData = capacityMatchService.getAuthencationData(approveStatus);
        return ResultUtil.success(authencationData);
    }

    //厂家提交申请
    @PostMapping("submitApplication")
    public Result submitApplication(@RequestBody String data) {
        Result result = capacityMatchService.submitApplication(data);
        return result;
    }

    //华三第一次审核
    @PostMapping("h3cConfirm")
    public Result h3cConfirm(@RequestBody String data) {
        Result result = capacityMatchService.submitApplication(data);
        return result;
    }

    @PostMapping("/getBoardList")
    public Result getBoardList(String version,String searchK,String searchV) throws Exception {
        Result result = capacityMatchService.getBoardList(version,searchK,searchV);
        return result;
    }

    @PostMapping("/getApproveProcess")
    public Result getApproveProcess(Integer id) {
        List<PcbOpenCapacityApproveProcess> approveProcess = capacityMatchService.getApproveProcess(id);
        return ResultUtil.success(approveProcess);
    }

    @PostMapping("/outputMatchFactoryValue")
    public Result outputMatchFactoryValue (@RequestBody List<Integer> factoryInfos) {
        Result result = capacityMatchService.outputMatchFactoryValue(factoryInfos, 1);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);  //InputStream in = url.openStream();
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);

            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }

    @PostMapping("/outputMatchFactoryValueAll")
    public Result outputMatchFactoryValueAll (@RequestBody List<Integer> factoryInfos) {
        Result result = capacityMatchService.outputMatchFactoryValue(factoryInfos, 2);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);  //InputStream in = url.openStream();
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);

            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }

    @PostMapping("/outputBoardList")
    public Result outputBoardList(String version){
        final Result result = capacityMatchService.outputBoardList(version);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);  //InputStream in = url.openStream();
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);

            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }

}
